package com.example.spring_config.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.spring_config.bean.LayuiTree;
import com.example.spring_config.dao.RoleDao;
import com.example.spring_config.mapper.Role;
import com.example.spring_config.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleDao, Role> implements RoleService {

    @Autowired
    private RoleDao roleDao;

    /**
     * 根据用户id获取角色集合
     * @param uid
     * @return
     */
    @Override
    public List<LayuiTree> getRoleByUser(String uid) {
        List<Role> list = list();
        List<Role> roleList = roleDao.getRoleList(uid);
        List<LayuiTree> arr = new ArrayList<>();
        for (Role role : list) {
            LayuiTree layuiTree = new LayuiTree();
            layuiTree.setName(role.getName());
            layuiTree.setValue(role.getId());
            for (Role role1 : roleList) {
                if(role1.getId().equals(role.getId())){
                    layuiTree.setSelected(true);
                    break;
                }
            }
            arr.add(layuiTree);
        }
        return arr;
    }

    @Override
    public List<String> getRoleNameByUid(String uid) {
        return roleDao.getRoleNameByUid(uid);
    }
}
